package com.jixiantubu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gemptc.bean.Photo_Library;
import com.jixiantubu.entity.Post_InfoDaoimp;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class FatieJieshou
 */
@WebServlet("/fatieJieshou2")
public class FatieJieshou2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FatieJieshou2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter printWriter = response.getWriter();
		Post_InfoDaoimp post = new Post_InfoDaoimp();
		List<Photo_Library> imgs = new ArrayList<Photo_Library>();
		
			SmartUpload smartUpload = new SmartUpload();
			com.jspsmart.upload.Files files = smartUpload.getFiles();
			smartUpload.initialize(this.getServletConfig(), request, response);
			try {
				smartUpload.upload();
				// 得到手机端传来的t_tittle
				String title = smartUpload.getRequest().getParameter("t_title");
				// 得到手机端传来的t_content
				String content = smartUpload.getRequest().getParameter(
						"t_content");
				String user_id = smartUpload.getRequest().getParameter(
						"user_id");
				// 得到上传的t_time
				
				Date current = new Date();
				String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.format(current);
				System.out.println(title+content+user_id+date);
				boolean a=false;
				boolean isSuccess = post.insert(Integer.parseInt(user_id),
						title, content, date,a);
				System.out.println(isSuccess);
				if (isSuccess) {
					//printWriter.write("success");
					int topicid=post.selectTopicid(date);
					for (int i = 0; i < files.getCount(); i++) {
						com.jspsmart.upload.File file = files.getFile(i);
						if (!file.isMissing()) {
							boolean falg=post.insertPhoto(Integer.parseInt(user_id), topicid, date);
							System.out.println(falg);
							if (falg) {
								int phid=post.selectPhid(date);
								SimpleDateFormat sdf = new SimpleDateFormat(
										"yyyyMMddHHmmssSSS");
								String pl_path = sdf.format(new java.util.Date());
								pl_path = pl_path + "." + file.getFileExt();// 得到文件的扩展名
								/*String filename =this.getServletContext().getRealPath("/")
										+ "img\\" + pl_path;*/
								String filename = "D:\\workspace\\Jinnang7\\WebContent\\img\\"
										+ pl_path;
								System.out.println("文件路径：" + filename);
								file.saveAs(filename);
								imgs.add(new Photo_Library(Integer.parseInt(user_id), phid, 0,pl_path));
								System.out.println(imgs);
								post.insertPhotoLibary(imgs);
							} 						
						}
					}
				} else {
					printWriter.write("erro");
				}
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
	}

}
